﻿
using hdb;
using hcache;
using hentity;
using hq;
using System;
using System.Collections.Generic;
using System.Web.Http;
using hdal;
using htaskCommon;
using hhttp;

namespace hwms {
    [RoutePrefix("Task")]
    public class TaskServer:BaseApiController {
        ICoreTaskServer coreTaskServer = WMSDalFactray.getDal<ICoreTaskServer>();
    
        [HttpGet, Route("all")]
        public BaseResult all() {

            return BaseResult.Ok(new {tasks= TaskCacheDB.taskCacheDict ,param=CompleteParamCache .taskCacheList,estimata=EstimateCache .getAll()});
        }
        ///Task/getComplete
        [HttpGet,  Route("getComplete")]
        public BaseResult getComplete() {
           return BaseResult.Ok(coreTaskServer.GetAll<CoreTask>("and  status=2"));
        }
    
         [HttpGet,  Route("GetCompleteParam/{id}")]
        public BaseResult GetCompleteParam(long id)
        {
            return BaseResult.Ok(CompleteParamCache.GetCompleteParam(id));
        }
         [HttpGet, Route("finishTask/{stn}/{taskNo}")]
        public BaseResult finishTask(int taskNo ,int stn) {
             Node node =  TransferConfigFacade.getConfig(stn);
            CoreTaskParam p1 = CompleteParamCache.getCompleteParamByTaskNo(taskNo);
            if( node == null ) {
                return BaseResult.Ok("站台不存在");
            } else {
            
               bool arived= TransferConfigFacade.isArived(stn ,p1.des ,null);
                if( !arived ) {
                    return BaseResult.Ok("路径不可达"+ stn +"=>"+p1.des);
                }
            }
            int connt = coreTaskServer.updateParamAt(p1.id ,stn);
            CoreTaskParam p = CompleteParamCache.getParamById(p1.id);
            if( p.des == stn ) {
                return BaseResult.Ok(connt == 1);
            }
          
            return BaseResult.Ok(connt==1);
        }

       
        [HttpGet, Route("getComplete/{taskNo}")]
        public BaseResult getComplete(int taskNo) {
            CoreTaskParam p1 = CompleteParamCache.getCompleteParamByTaskNo(taskNo);
            CoreTask complete = coreTaskServer.getById<CoreTask>(p1.taskId);
            return BaseResult.Ok(complete);
        }
        [HttpGet, Route("move/{src}/{des}")]
        public BaseResult move(int src ,int des) {
            WMSBaseTransFacade.doWcsMoveTask(WMSIdUtil.getId() ,WMSIdUtil.getId() ,src ,des);
            return BaseResult.Ok("任务生成");
        }

        [HttpGet, Route("DeleteCraneAndCarrierTask/{taskNo}")]
        public bool DeleteCraneAndCarrierTask(int taskNo) {
          
                CoreTaskParam p = CompleteParamCache.getCompleteParamByTaskNo(taskNo);
                long paramId = p.id;
                TransRes res= WMSBaseTransFacade.doDeleteTaskEngine(paramId);
                if( res.s ) {
                    CacheFacade.removeComplete(p.taskId);
               
                }
                 return true;
            
         }
         [HttpGet,  Route("getTaskByCompleteId/{deviceType}/{id}")]
        public BaseResult getTaskByCompleteId(String deviceType ,long id) {
            Type t  = TaskRegisterUtil.getType(deviceType);
            return BaseResult.Ok(TaskCacheDB.getTaskByCompleteId( t,id));
        }
        [HttpGet,  Route("carrier/{taskNos}")]
        public BaseResult getCarrierkBytaskNo(String taskNos) {
            String[] taskArr = taskNos.Split(',');
            List<TaskCarrier> list = new List<TaskCarrier>();
            foreach( String t in taskArr ) {
                TaskCarrier taskCarrier = TaskCacheDB.getTaskByTaskNo<TaskCarrier>(int.Parse(t));
                if( taskCarrier != null ) {
                    list.Add(taskCarrier);
                }
               
            }
             
            return BaseResult.Ok(list);
        }
     
         [HttpGet,  Route("getAllTaskByCompleteId/{id}")]
        public BaseResult getAllTaskByCompleteId(long id) {
            List<Type> types = TaskRegisterUtil.getAllTaskType();
            Dictionary<String ,List<Object>> dict = new Dictionary<string ,List<object>>();
            foreach( Type t in types ) {
                List<BaseTask> list = TaskCacheDB.getTaskByCompleteId(t ,id);
                String name =  t.Name;
                List<object> res = new List<object>();
                foreach( BaseTask bt in list) {
                
                
                    res.Add(bt.Clone());
                }
                dict.Add(name ,res);
            }
            List<CoreTaskParam> paramList = CompleteParamCache.GetCompleteParam(id);
            List<object> param1 = new List<object>();
            foreach( CoreTaskParam p in paramList ) {
                param1.Add(p);
            }
            dict.Add("completeParam" ,param1);
            return BaseResult.Ok(dict);
        }
        
    
        [HttpGet, Route("node")]
        public BaseResult node() {

            return BaseResult.Ok(TransferConfigFacade.getAllNode());
        }

        [HttpGet, Route("e")]
        public BaseResult e() {

            return BaseResult.Ok(EstimateCache.getAll());
        }
        [HttpGet, Route("t/{taskNo}")]
        public BaseResult t(int taskNo) {

            return BaseResult.Ok(EstimateCache.getEstimates(taskNo));
        }
    }
 }
